home *** CD-ROM | disk | FTP | other *** search
/ Aminet 31 / Aminet 31 (1999)(Schatztruhe)[!][Jun 1999].iso / Aminet / util / libs / regex.lha / regex / Docs / regex.txt < prev   
Encoding:
Text File  |  1999-04-23  |  13.9 KB  |  307 lines

  1. Name:   REGEX 3
  2. Date    17 May 1993
  3. Author: Henry Spencer
  4.         AmigaOS shared library port by Alfonso Ranieri
  5.  
  6. regcomp
  7. regexec
  8. regerror
  9. regfree
  10.  
  11. SYNOPSIS
  12.  
  13. int regcomp(regex_t *preg, const char *pattern, int cflags);
  14. int regexec(const regex_t *preg, const char *string,
  15.             size_t nmatch, regmatch_t pmatch[], int eflags);
  16. size_t regerror(int errcode, const regex_t *preg,
  17. char *errbuf, size_t errbuf_size);
  18. void regfree(regex_t *preg);
  19.  
  20.  
  21. DESCRIPTION
  22. These routines implement POSIX 1003.2 regular expressions (REs);
  23. - regcomp  compiles an RE written as a string into an internal form
  24. - regexec  matches that internal form against a string and reports results
  25. - regerror transforms error codes from either into human-readable messages
  26. - regfree  frees any dynamically-allocated storage used by the internal form of an RE.
  27.  
  28. The header <regex.h>
  29. declares two structure types:
  30. - regex_t
  31. - regmatch_t
  32. The former for compiled internal forms and the latter for match reporting.
  33. It also declares the four functions, a type regoff_t,
  34. and a number of constants with names starting with REG_ .
  35.  
  36. regcomp
  37. compiles the regular expression contained in the pattern string,
  38. subject to the flags in cflags and places the results in the
  39. regex_t structure pointed to by preg .
  40.  
  41. Cflags
  42. is the bitwise OR of zero or more of the following flags:
  43. REG_EXTENDED Compile modern (``extended'') REs,
  44.              rather than the obsolete (``basic'') REs that
  45.              are the default.
  46. REG_BASIC    This is a synonym for 0, provided as a counterpart
  47.              to REG_EXTENDED to improve readability.
  48. REG_NOSPEC   Compile with recognition of all special characters
  49.              turned off. All characters are thus considered ordinary,
  50.              so the ``RE'' is a literal string.
  51.              This is an extension, compatible with but not specified
  52.              by POSIX 1003.2, and should be used with caution in software
  53.              intended to be portable to other systems.
  54.              REG_EXTENDED and REG_NOSPEC may not be used in the same call
  55.              to regcomp .
  56. REG_ICASE    Compile for matching that ignores upper/lower case distinctions.
  57. REG_NOSUB    Compile for matching that need only report success or failure,
  58.              not what was matched.
  59. REG_NEWLINE  Compile for newline-sensitive matching. By default, newline
  60.              is a completely ordinary character with no special meaning
  61.              in either REs or strings. With this flag, `[^' bracket
  62.              expressions and `.' never match newline, a `^' anchor matches
  63.              the null string after any newline in the string in addition to
  64.              its normal function, and the `$' anchor matches the null string
  65.              before any newline in the string in addition to its normal
  66.              function.
  67. REG_PEND     The regular expression ends, not at the first NUL, but just
  68.              before the character pointed to by the re_endp member of the
  69.              structure pointed to by preg . The re_endp member is of type
  70.              const char * . This flag permits inclusion of NULs in the RE;
  71.              they are considered ordinary characters.
  72.              This is an extension, compatible with but not specified
  73.              by POSIX 1003.2, and should be used with caution in software
  74.              intended to be portable to other systems.
  75.  
  76. When successful, regcomp returns 0 and fills in the structure pointed to by
  77. preg . One member of that structure (other than re_endp) is publicized:
  78. re_nsub , of type size_t , contains the number of parenthesized
  79. subexpressions within the RE (except that the value of this member is
  80. undefined if the REG_NOSUB flag was used).
  81. If regcomp fails, it returns a non-zero error code;
  82. see DIAGNOSTICS.
  83.  
  84. regexec
  85. matches the compiled RE pointed to by preg against the string ,
  86. subject to the flags in eflags , and reports results using nmatch ,
  87. pmatch , and the returned value.
  88. The RE must have been compiled by a previous invocation of regcomp .
  89. The compiled form is not altered during execution of regexec ,
  90. so a single compiled RE can be used simultaneously by multiple threads.
  91.  
  92. By default, the NUL-terminated string pointed to by string
  93. is considered to be the text of an entire line, minus any terminating
  94. newline. The eflags argument is the bitwise OR of zero or more of the
  95. following flags:
  96. - REG_NOTBOL   The first character of the string is not the beginning of
  97.                a line, so the `^' anchor should not match before it.
  98.                This does not affect the behavior of newlines under REG_NEWLINE.
  99. - REG_NOTEOL   The NUL terminating the string does not end a line, so the
  100.                `$' anchor should not match before it. This does not affect
  101.                the behavior of newlines under REG_NEWLINE.
  102. - REG_STARTEND The string is considered to start at
  103.                string + pmatch[0].rm_so
  104.                and to have a terminating NUL located at
  105.                string + pmatch[0].rm_eo
  106.                (there need not actually be a NUL at that location),
  107.                regardless of the value of nmatch .
  108.                See below for the definition of pmatch and nmatch .
  109.                This is an extension, compatible with but not specified
  110.                by POSIX 1003.2, and should be used with caution in software
  111.                intended to be portable to other systems.
  112.                Note that a non-zero rm_so does not imply REG_NOTBOL;
  113.                affects only the location of the string, not how it is
  114.                matched.
  115.  
  116. Normally, regexec returns 0 for success and the non-zero code REG_NOMATCH
  117. for failure. Other non-zero error codes may be returned in exceptional
  118. situations; see DIAGNOSTICS.
  119.  
  120. If REG_NOSUB was specified in the compilation of the RE, or if nmatch
  121. is 0, regexec ignores the pmatch argument (but see below for the case
  122. where REG_STARTEND is specified). Otherwise, pmatch points to an array of
  123. nmatch structures of type regmatch_t . Such a structure has at least the
  124. members rm_so and rm_eo , both of type regoff_t (a signed arithmetic type
  125. at least as large as an off_t and a ssize_t ), containing respectively the
  126. offset of the first character of a substring and the offset of the first
  127. character after the end of the substring.
  128. Offsets are measured from the beginning of the string argument given to
  129. regexec .
  130. An empty substring is denoted by equal offsets, both indicating the
  131. character following the empty substring.
  132.  
  133. The 0th member of the pmatch array is filled in to indicate what substring
  134. of string was matched by the entire RE.
  135. Remaining members report what substring was matched by parenthesized
  136. subexpressions within the RE;
  137. member i reports subexpression i , with subexpressions counted
  138. (starting at 1) by the order of their opening parentheses in the RE, left
  139. to right. Unused entries in the array(emcorresponding either to
  140. subexpressions that did not participate in the match at all, or to
  141. subexpressions that do not exist in the RE (that is,
  142. i > preg->re_nsub)(emhave both rm_so and rm_eo set to -1.
  143. If a subexpression participated in the match several times, the reported
  144. substring is the last one it matched.
  145. (Note, as an example in particular, that when the RE `(b*)+' matches `bbb',
  146. the parenthesized subexpression matches each of the three `b's and then
  147. an infinite number of empty strings following the last `b',
  148. so the reported substring is one of the empties.)
  149.  
  150. If REG_STARTEND is specified, pmatch must point to at least one regmatch_t
  151. (even if nmatch is 0 or REG_NOSUB was specified), to hold the input offsets
  152. for REG_STARTEND. Use for output is still entirely controlled by nmatch ;
  153. if nmatch is 0 or REG_NOSUB was specified, the value of pmatch [0] will not
  154. be changed by a successful regexec .
  155.  
  156. regerror
  157. maps a non-zero errcode from either regcomp or regexec to a human-readable,
  158. printable message. If preg is non-NULL, the error code should have arisen
  159. from use of the regex_t pointed to by preg , and if the error code came
  160. from regcomp , it should have been the result from the most recent regcomp
  161. using that regex_t .
  162. ( regerror may be able to supply a more detailed message using information
  163. from the regex_t ).
  164. regerror places the NUL-terminated message into the buffer pointed to by
  165. errbuf , limiting the length (including the NUL) to at most errbuf_size
  166. bytes. If the whole message won't fit, as much of it as will fit before
  167. the terminating NUL is supplied. In any case, the returned value is the
  168. size of buffer needed to hold the whole message (including terminating NUL).
  169. If errbuf_size is 0, errbuf is ignored but the return value is still correct.
  170.  
  171. If the errcode given to regerror is first ORed with REG_ITOA,
  172. the ``message'' that results is the printable name of the error code,
  173. e.g. ``REG_NOMATCH'', rather than an explanation thereof.
  174. If errcode is REG_ATOI, then preg shall be non-NULL and the re_endp
  175. member of the structure it points to must point to the printable name
  176. of an error code; in this case, the result in errbuf is the decimal digits
  177. of the numeric value of the error code (0 if the name is not recognized).
  178. REG_ITOA and REG_ATOI are intended primarily as debugging facilities;
  179. they are extensions, compatible with but not specified by POSIX 1003.2,
  180. and should be used with caution in software intended to be portable to
  181. other systems.
  182. Be warned also that they are considered experimental and changes are possible.
  183.  
  184. regfree
  185. frees any dynamically-allocated storage associated with the compiled RE
  186. pointed to by preg . The remaining regex_t is no longer a valid compiled
  187. RE and the effect of supplying it to regexec or regerror is undefined.
  188.  
  189. None of these functions references global variables except for tables
  190. of constants; all are safe for use from multiple threads if the arguments
  191. are safe.
  192.  
  193. IMPLEMENTATION CHOICES
  194. There are a number of decisions that 1003.2 leaves up to the implementor,
  195. either by explicitly saying ``undefined'' or by virtue of them being
  196. forbidden by the RE grammar. This implementation treats them as follows.
  197.  
  198. There is no particular limit on the length of REs, except insofar as memory
  199. is limited. Memory usage is approximately linear in RE size, and largely
  200. insensitive to RE complexity, except for bounded repetitions.
  201. See BUGS for one short RE using them that will run almost any system out
  202. of memory.
  203.  
  204. A backslashed character other than one specifically given a magic meaning
  205. by 1003.2 (such magic meanings occur only in obsolete [``basic''] REs)
  206. is taken as an ordinary character.
  207.  
  208. Any unmatched [ is a REG_EBRACK error.
  209.  
  210. Equivalence classes cannot begin or end bracket-expression ranges.
  211.  
  212. The endpoint of one range cannot begin another.
  213.  
  214. RE_DUP_MAX, the limit on repetition counts in bounded repetitions, is 255.
  215.  
  216. A repetition operator (?, *, +, or bounds) cannot follow another repetition
  217. operator.
  218.  
  219. A repetition operator cannot begin an expression or subexpression
  220. or follow `^' or `|'.
  221.  
  222. `|' cannot appear first or last in a (sub)expression or after another `|',
  223. i.e. an operand of `|' cannot be an empty subexpression.
  224.  
  225. An empty parenthesized subexpression, `()', is legal and matches an
  226. empty (sub)string.
  227.  
  228. An empty string is not a legal RE.
  229.  
  230. A `{' followed by a digit is considered the beginning of bounds for a
  231. bounded repetition, which must then follow the syntax for bounds.
  232. A `{' not followed by a digit is considered an ordinary character.
  233.  
  234. `^' and `$' beginning and ending subexpressions in obsolete (``basic'')
  235. REs are anchors, not ordinary characters.
  236.  
  237. DIAGNOSTICS
  238. Non-zero error codes from regcomp and regexec include the following:
  239.  
  240. - REG_NOMATCH   regexec() failed to match
  241. - REG_BADPAT    invalid regular expression
  242. - REG_ECOLLATE  invalid collating element
  243. - REG_ECTYPE    invalid character class
  244. - REG_EESCAPE   e applied to unescapable character
  245. - REG_ESUBREG   invalid backreference number
  246. - REG_EBRACK    brackets [ ] not balanced
  247. - REG_EPAREN    parentheses ( ) not balanced
  248. - REG_EBRACE    braces { } not balanced
  249. - REG_BADBR     invalid repetition count(s) in { }
  250. - REG_ERANGE    invalid character range in [ ]
  251. - REG_ESPACE    ran out of memory
  252. - REG_BADRPT    ?, *, or + operand invalid
  253. - REG_EMPTY     empty (sub)expression
  254. - REG_ASSERT    ``can't happen''(emyou found a bug
  255. - REG_INVARG    invalid argument, e.g. negative-length string
  256.  
  257. HISTORY
  258. Written by Henry Spencer at University of Toronto, henry@zoo.toronto.edu.
  259. Ported to Amiga OS as shared library by Alfonso Ranieri <alfier@iol.it>
  260.  
  261. BUGS
  262. This is an alpha release with known defects.
  263. Please report problems.
  264.  
  265. There is one known functionality bug.
  266. The implementation of internationalization is incomplete:
  267. the locale is always assumed to be the default one of 1003.2,
  268. and only the collating elements etc. of that locale are available.
  269.  
  270. The back-reference code is subtle and doubts linger about its correctness
  271. in complex cases.
  272.  
  273. regexec performance is poor. This will improve with later releases.
  274.  
  275. Nmatch exceeding 0 is expensive;
  276. nmatch exceeding 1 is worse.
  277. regexec is largely insensitive to RE complexity except that back
  278. references are massively expensive.
  279. RE length does matter; in particular, there is a strong speed bonus
  280. for keeping RE length under about 30 characters, with most special
  281. characters counting roughly double.
  282.  
  283. regcomp implements bounded repetitions by macro expansion, which is
  284. costly in time and space if counts are large or bounded repetitions
  285. are nested.
  286. An RE like, say,
  287. `((((a{1,100}){1,100}){1,100}){1,100}){1,100}'
  288. will (eventually) run almost any existing machine out of swap space.
  289.  
  290. There are suspected problems with response to obscure error conditions.
  291. Notably, certain kinds of internal overflow, produced only by truly
  292. enormous REs or by multiply nested bounded repetitions, are probably
  293. not handled well.
  294.  
  295. Due to a mistake in 1003.2, things like `a)b' are legal REs because `)' is
  296. a special character only in the presence of a previous unmatched `('.
  297. This can't be fixed until the spec is fixed.
  298.  
  299. The standard's definition of back references is vague.
  300. For example, does
  301. `ae(e(be)*e2e)*d' match `abbbd'?
  302. Until the standard is clarified,
  303. behavior in such cases should not be relied on.
  304.  
  305. The implementation of word-boundary matching is a bit of a kludge,
  306. and bugs may lurk in combinations of word-boundary matching and anchoring.
  307.